package com.edu.servlet;

import com.edu.entity.User;
import com.edu.service.productOrderService;
import com.edu.service.impl.productOrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/deleteProduct")
public class DeleteProductServlet extends HttpServlet {
    private productOrderService orderService = new productOrderServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        System.out.println("请求参数: " + request.getParameterMap().toString());
        PrintWriter out = response.getWriter();
        
        String goodsIdStr = request.getParameter("goodsId");
        System.out.println("Delete request received for goodsId: " + goodsIdStr);
        
        if (goodsIdStr == null || goodsIdStr.isEmpty()) {
            out.println("{\"success\": false, \"message\": \"商品ID不能为空\"}");
            return;
        }
        
		/*
		 * HttpSession session = request.getSession(); User user = (User)
		 * session.getAttribute("logInUser");
		 */
        
        try {
            int goodsId = Integer.parseInt(goodsIdStr);
            System.out.println("Parsed goodsId: " + goodsId + ", SellerId: " + 1);//user.getUserid());
            
            boolean result = orderService.offShelfgoods(goodsId,1);// user.getUserid());
            
            if (result) {
                out.println("{\"success\": true}");
            } else {
                out.println("{\"success\": false, \"message\": \"删除失败，请重试\"}");
            }
        } catch (NumberFormatException e) {
            out.println("{\"success\": false, \"message\": \"商品ID格式错误\"}");
        }
    }
}